package com.bhs.sansonglogistics.ui.message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.CircleFriendsBean;
import com.bhs.sansonglogistics.bean.CircleJudgeList;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.CircleFriendsAdapter;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 圈子详情
 */
public class CircleDetailActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private RecyclerView mRvList;
    private CircleFriendsAdapter mAdapter;
    private String myUnid;
    private CircleFriendsBean circleFriendsBean;
    private CircleReuse circleReuse;

    @Override
    public int initLayout() {
        return R.layout.activity_circle_detail;
    }

    private List<CircleFriendsBean> mList = new ArrayList<>();

    @Override
    public void initView() {
        myUnid = SharedPreferencesUtils.getString(mActivity, "uid");
        circleFriendsBean = (CircleFriendsBean) getIntent().getSerializableExtra("data");
        mList.add(circleFriendsBean);
        TextView mTvDelete = findViewById(R.id.tv_delete);
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new CircleFriendsAdapter(R.layout.item_circle_friends2, mActivity);
        if (circleFriendsBean.getUnid().equals(myUnid)) {
            mTvDelete.setVisibility(View.VISIBLE);
            mTvDelete.setOnClickListener(this);
        }
    }

    @Override
    public void initData() {
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter.setNewData(mList);
        circleReuse = new CircleReuse(mActivity, myUnid);
        circleReuse.clickEvent(mAdapter);
        networkRequest(netApi.circleJudgeList(circleFriendsBean.getId()), this, 888);
    }

    @Override
    public void onClick(View view) {
        new XPopup.Builder(mActivity)
                .asConfirm("提示", "确定删除吗",
                        "取消", "确定",
                        new OnConfirmListener() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void onConfirm() {
                                loadingPopupShow();
                                networkRequest(netApi.delCircle(circleFriendsBean.getId()), CircleDetailActivity.this, 9527);
                            }
                        }, null, false)
                .show();
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 888) {
            CircleJudgeList judgeList = new Gson().fromJson(json, CircleJudgeList.class);
            if (judgeList.getCode().equals("1")) {
                mList.get(0).setJudgeList(judgeList.getData().getList());
                mAdapter.notifyItemChanged(0,0);
            }
        } else {
            loadingPopupDismiss();
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            if (bean.getCode().equals("1")) {
                Intent intent = new Intent();
                setResult(2, intent);
                finish();
            } else {
                ToastUtil.show(bean.getMsg());
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data", mAdapter.getData().get(0));
        setResult(1, intent);
        finish();
    }
}